<?php

namespace app\api\controller;

use app\api\model\Attention;
use app\api\model\Blacklist;
use app\api\model\Code;
use app\api\model\ConfigSystem as Config;
use app\api\model\Dymanic;
use app\api\model\Privatechat;
use app\api\model\UserClaimAddress;
use app\api\model\YaoqingRecords;
use think\Db;
use think\Validate;

class Activity extends Common {
    /**
     * @param $content
     * @param $images
     * @param $activity_time
     * @param $private
     * @param $sizes
     * @param $limit_uid
     * @param $sex
     * @param $pay
     * @param $address_name
     * @param $address_desc
     * @param $longitude
     * @param $latitude
     * @param $city
     * @return mixed
     */
    public function create($content, $images, $activity_time, $private = 0, $sizes = 0, $limit_uid = 0, $sex = 0, $pay = 0, $address_name = '', $address_desc = '', $longitude = '', $latitude = '', $city = '', $category = 0, $price = 0) {
        $timenow   = time();
        $limit_uid = $limit_uid && $limit_uid == $this->member['id'] ? 0 : $limit_uid;
        $rule      = [
            'content'       => 'require|max:150',
            'images'        => 'require|max:800',
            'activity_time' => 'require|after:' . date('Y-m-d H:i:s', $timenow),
            'private'       => 'require|in:0,1',
            'sizes'         => 'require|in:0,1',
            'sex'           => 'in:0,1,2',
            'pay'           => 'in:0,1',
            'address_name'  => 'require',
            'address_desc'  => 'require',
            'longitude'     => 'require',
            'latitude'      => 'require',
            'city'          => 'require',
        ];

        $msg = [
            'content.require'       => '请填写活动内容',
            'content.max'           => '活动内容长度超出',
            'images.require'        => '请补充活动展示图',
            'images.max'            => '活动展示图超出',
            'activity_time.require' => '请选择活动时间',
            'activity_time.after'   => '活动时间错误',
            'private.require'       => '状态选择错误',
            'private.in'            => '状态选择错误',
            'sizes.require'         => '人数选择错误',
            'sizes.in'              => '人数选择错误',
            'sex.require'           => '性别选择错误',
            'sex.in'                => '性别选择错误',
            'pay.require'           => '支付方选择错误',
            'pay.in'                => '支付方选择错误',
            'address_name.require'  => '地址选择错误',
            'address_desc.require'  => '地址选择错误',
            'longitude.require'     => '地址选择错误',
            'latitude.require'      => '地址选择错误',
            'city.require'          => '地址选择错误',
        ];
        $data = [
            'content'       => $content,
            'images'        => $images,
            'activity_time' => $activity_time,
            'private'       => $private,
            'sizes'         => $sizes,
            'sex'           => $sex,
            'pay'           => $pay,
            'address_name'  => $address_name,
            'address_desc'  => $address_desc,
            'longitude'     => $longitude,
            'latitude'      => $latitude,
            'city'          => $city,
            'limit_uid'     => $limit_uid,
            'user_id'       => $this->member['id'],
            'status'        => 1,
            'category'      => $category,
            'price'         => $price,
        ];
            return $this->miss('暂未开放');
        if ($this->member['age'] < 18) {
            return $this->miss('未成年暂不支持发布邀约活动');
        }
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)) {
            return $this->miss($validate->getError());
        }
        if ($pay == 1 && $price <= 0) {
            return $this->miss('请输入报名费用');
        }
        $data['address_id'] = UserClaimAddress::FindByCreate(['name' => $address_name, 'address' => $address_desc, 'longitude' => $longitude, 'latitude' => $latitude, 'city' => $city]);
        if ($data['private'] == 0) {
            $config         = Config::index('check');
            $data['status'] = $config['check_activity_create'] ? 0 : 1;
        }

        $data['create_time'] = $data['update_time'] = $timenow;

        $result = Db::name('activity_offline')->insertGetId($data);
        if ($result) {
            if ($data['private'] == 0) {
                Dymanic::add([
                    'userid'      => $data['user_id'],
                    'content'     => $data['content'],
                    'sumbImage'   => $data['images'],
                    'masterimg'   => $data['images'],
                    'citys'       => $data['city'],
                    'source_type' => 4,
                    'source_id'   => $result,
                    'shenhe'      => $data['status'] ? 1 : 2,
                ]);
            }
            return $this->pass('发布成功' . ($data['private'] == 0 && $data['status'] == 0 ? ',待审核通过后方可公开展示' : ''), 'data', ['id' => $result]);
        }
        return $this->miss('发布失败');
    }

    /**
     * @param $id
     * @return mixed
     */
    public function info($id) {
        $data = Db::name('activity_offline')
            ->where(['id' => $id, 'delete_time' => 0])
            ->field('update_time,delete_time', 'filter')
            ->find();
        if (!$data) {
            return $this->miss('活动不存在');
        }
        if ($data['private']) {
            if ($this->member['id'] == $data['user_id']) {

            } elseif ($this->member['id'] == $data['limit_uid']) {
                unset($data['status'], $data['reason']);
            } else {
                return $this->miss('活动不存在');
            }
        } else {
            if ($data['status'] != 1) {
                return $this->miss('活动不存在');
            }
            unset($data['status'], $data['reason']);
        }
        if ($this->member['id'] == $data['user_id']) {
            $data['joinUsers'] = Db::name('activity_join')
                ->alias('a')
                ->join('user b', 'b.id=a.user_id')
                ->where(['a.active_id' => $id, 'a.paystate' => 1])
                ->field('b.id,b.name,b.head_portrait,a.create_time join_time')
                ->select();
            foreach ($data['joinUsers'] as $key => $value) {
                $data['joinUsers'][$key]['join_time'] = date('Y-m-d H:i', $value['join_time']);
            }
        }
        $data['images']        = explode(',', $data['images']);
        $data['distance']      = _distance2($this->member['longitude'], $this->member['latitude'], $data['longitude'], $data['latitude']);
        $data['user']          = _user_cache($data['user_id']);
        $data['activity_time'] = date('Y-m-d H:i', strtotime($data['activity_time']));
        $data['time']          = $data['create_time'];
        $data['create_time']   = _time($data['create_time']);
        $data['store']         = Db::name('user_claim_address')->where(['id' => $data['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
        if ($data['store']) {
            $data['store']['distance'] = _distance2($this->member['longitude'], $this->member['latitude'], $data['store']['longitude'], $data['store']['latitude']);
            unset($data['store']['longitude'], $data['store']['latitude']);
        } else {
            $data['store'] = false;
        }
        unset($data['address_id'], $data['longitude'], $data['latitude']);

        $data['joinInfo'] = Db::name('activity_join')->where(['user_id' => $this->member['id'], 'active_id' => $id, 'paystate' => 1])->find();
        $data['is_join']  = $data['joinInfo'] ? true : false;

        return $this->pass($data);
    }

    /**
     * @param $id
     * @param $timestamp
     * @return mixed
     */
    public function infoul($id, $timestamp = '') {
        $data = Db::name('activity_offline')
            ->where(['id' => $id, 'delete_time' => 0])
            ->field('update_time,delete_time', 'filter')
            ->find();
        if (!$data) {
            return $this->miss('活动不存在');
        }
        if ($data['private']) {
            if ($timestamp != $data['create_time']) {
                return $this->miss('活动不存在');
            }
        } else {
            if ($data['status'] != 1) {
                return $this->miss('活动不存在');
            }
        }
        $data['images']        = explode(',', $data['images']);
        $data['user']          = _user_cache($data['user_id']);
        $data['activity_time'] = date('Y-m-d H:i', strtotime($data['activity_time']));
        $data['time']          = $data['create_time'];
        $data['create_time']   = _time($data['create_time']);
        $data['store']         = Db::name('user_claim_address')->where(['id' => $data['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
        if ($data['store']) {
            unset($data['store']['longitude'], $data['store']['latitude']);
        } else {
            $data['store'] = false;
        }
        unset($data['address_id'], $data['longitude'], $data['latitude'], $data['status'], $data['reason']);

        return $this->pass($data);
    }

    /**
     * @param $page
     * @param $pagesize
     * @param $nav
     * @param $state
     * @return mixed
     */
    public function index($page = 1, $pagesize = 20, $nav = 1, $state = 0, $category = 0, $sex = 0, $city = '', $pay = '') {
        $config    = Config::where(['category' => ['in', ['kefu', 'user']]])->column('title, content');
        $blacklist = Blacklist::lists($this->member['id'], false);
        if ($blacklist && $blacklist[0]) {
            $blacklist[] = $config['id'];
        } else {
            $blacklist = [$config['id']];
        }
        $where = ['a.private' => 0, 'a.user_id' => ['not in', $blacklist], 'a.delete_time' => 0, 'status' => 1];
        if ($nav == 1) {

        } elseif ($nav == 3) {
            $attentions       = Attention::lists($this->member['id'], 1);
            $where['a.user_id'] = ['in', $attentions];
        } elseif ($nav == 2) {
            $where['a.city'] = $this->member['city'];
        }
        if ($state) {
            $where['a.state'] = 0;
        }
        if ($category) {
            $where['a.category'] = $category;
        }
        if ($city) {
            $where['a.city'] = $city;
        }
        if ($pay) {
            $where['a.pay'] = $pay - 1;
        }
        if ($sex) {
            $where['a.sex'] = ['in', [0, $sex]];
        }
        if ($this->member['sex'] == '男') {
            $where['b.age'] = ['between', [$this->member['age'] - 20, $this->member['age']]];
        } else {
            $where['b.age'] = ['between', [$this->member['age'] - 20, $this->member['age'] + 20]];
        }

        // $limitdatetime = '2023-04-15 00:00:00';
        // $limittime = strtotime($limitdatetime);
        // if (strtotime($this->member['register_datetime']) >= $limittime) {
        //     $where['b.register_datetime'] = ['>=', $limitdatetime];
        // } else {
        //     $where['b.register_datetime'] = ['<', $limitdatetime];
        // }

        $data = Db::name('activity_offline a')
            ->join('user b', 'b.id=a.user_id', 'left')
            ->where($where)
            ->where('a.sizes=1  or (a.sizes=0 and a.joins=0)')
            ->whereTime('activity_time', '>=', time())
            ->field('a.id,a.user_id,a.content,a.pay,a.longitude,a.latitude,a.address_name,a.address_id,a.activity_time,a.images,a.create_time,a.state,a.price')
            ->order('a.id desc')
            ->limit(($page - 1) * $pagesize, $pagesize)
            ->select();
        $users = Db::name('user')->where(['id' => ['in', array_column($data, 'user_id')]])->cache(60)->column('name,head_portrait,sex,age', 'id');
        foreach ($data as $key => $value) {
            $data[$key]['images']        = explode(',', $value['images'])[0];
            $data[$key]['distance']      = _distance2($this->member['longitude'], $this->member['latitude'], $value['longitude'], $value['latitude']);
            $data[$key]['user']          = $users[$value['user_id']];
            $data[$key]['join']          = Db::name('activity_join')->where(['active_id' => $value['id'], 'paystate' => 1])->count();
            $data[$key]['activity_time'] = date('Y-m-d H:i', strtotime($value['activity_time']));
            $data[$key]['create_time']   = _time($value['create_time']);
            $data[$key]['store']         = Db::name('user_claim_address')->where(['id' => $value['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
            if ($data[$key]['store']) {
                $data[$key]['store']['distance'] = _distance2($this->member['longitude'], $this->member['latitude'], $data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
                unset($data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
            } else {
                $data[$key]['store'] = false;
            }
        }
        return $this->pass($data);
    }

    /**
     * @param $page
     * @param $pagesize
     * @param $nav
     * @param $state
     * @return mixed
     */
    public function indexul($page = 1, $pagesize = 20, $nav = 1, $state = 0, $category = 0, $sex = 0, $city = '', $pay = '') {
        $config    = Config::where(['category' => ['in', ['kefu', 'user']]])->column('title, content');
        $blacklist = [$config['id']];
        $where     = ['private' => 0, 'user_id' => ['not in', $blacklist], 'delete_time' => 0, 'status' => 1];
        if ($nav != 1) {
            return $this->miss('请打开APP使用');
        }
        if ($state) {
            $where['state'] = 0;
        }
        if ($category) {
            $where['category'] = $category;
        }
        if ($city) {
            $where['city'] = $city;
        }
        if ($pay) {
            $where['pay'] = $pay - 1;
        }
        if ($sex) {
            $where['sex'] = ['in', [0, $sex]];
        }
        $data = Db::name('activity_offline')
            ->where($where)
            ->field('id,user_id,content,pay,longitude,latitude,address_name,address_id,activity_time,images,create_time,state,price')
            ->order('id desc')
            ->limit(($page - 1) * $pagesize, $pagesize)
            ->select();
        $users = Db::name('user')->where(['id' => ['in', array_column($data, 'user_id')]])->cache(60)->column('name,head_portrait,sex,age', 'id');
        foreach ($data as $key => $value) {
            $data[$key]['images']        = explode(',', $value['images'])[0];
            $data[$key]['distance']      = false;
            $data[$key]['user']          = $users[$value['user_id']];
            $data[$key]['join']          = Db::name('activity_join')->where(['active_id' => $value['id'], 'paystate' => 1])->count();
            $data[$key]['activity_time'] = date('Y-m-d H:i', strtotime($value['activity_time']));
            $data[$key]['create_time']   = _time($value['create_time']);
            $data[$key]['store']         = Db::name('user_claim_address')->where(['id' => $value['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
            if ($data[$key]['store']) {
                $data[$key]['store']['distance'] = false;
                unset($data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
            } else {
                $data[$key]['store'] = false;
            }
        }
        return $this->pass($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function setOver($id) {
        $timenow  = time();
        $activity = Db::name('activity_offline')->where(['id' => $id, 'user_id' => $this->member['id'], 'state' => 0, 'delete_time' => 0])->find();
        if (!$activity) {
            return $this->miss('设置失败');
        }
        $longitude = $this->member['longitude'];
        $latitude = $this->member['latitude'];
        if ($activity['longitude'] && $activity['latitude']) {
            if (!$longitude || !$latitude) {
                return $this->miss('位置未获取成功');
            }
            $distance = _distance($longitude, $latitude, $activity['longitude'], $activity['latitude'], 0);
            if ($distance > 100) {
                return $this->miss('当前位置距离活动地点较远');
            }
        }
        $result = Db::name('activity_offline')->where(['id' => $id])->update(['state' => 1, 'update_time' => $timenow]);
        if ($result) {
            if ($activity['pay'] == 1) {
                $joinUsers = Db::name('activity_join')->where(['active_id' => $id, 'paystate' => 1])->column('user_id');
                Db::name('activity_join')->where(['active_id' => $id, 'paystate' => 1])->update(['end_time' => $timenow + 60 * 60 * 24 * 7]);
                foreach ($joinUsers as $key => $value) {
                    Privatechat::notice($value, '您的付费参与的邀约活动【' . $activity['content'] . '】已结束，请尽快确认（若不处理，7天后将自动确认）。如有疑问，请咨询客服');
                }
            }
            return $this->pass('设置成功');
        }
        return $this->miss('设置失败');
    }

    /**
     * @param $id
     * @return mixed
     */
    public function dele($id) {
        $activity = Db::name('activity_offline')->where(['id' => $id, 'user_id' => $this->member['id'], 'delete_time' => 0])->find();
        if (!$activity) {
            return $this->miss('删除失败');
        }
        $result = Db::name('activity_offline')->where(['id' => $id])->update(['delete_time' => time()]);
        if ($result) {
            return $this->pass('删除成功');
        }
        return $this->miss('删除失败');
    }

    /**
     * @param $id
     * @return mixed
     */
    public function join($id, $paymethod = 3, $h5 = 0, $w2 = 0) {
        $activity = Db::name('activity_offline')->where(['id' => $id, 'state' => 0, 'delete_time' => 0])->find();
        // 活动不存在
        // 发布用户是自己
        // 私密且未邀请我
        if (!$activity || $activity['user_id'] == $this->member['id'] || ($activity['private'] && $activity['limit_uid'] != $this->member['id'])) {
            return $this->miss('操作失败');
        }
        $joinUsers = Db::name('activity_join')->where(['active_id' => $id, 'paystate' => 1])->column('user_id');

        if (in_array($this->member['id'], $joinUsers)) {
            if ($activity['pay'] == 1) {
                return $this->miss('付费活动暂不支持取消报名');
            }
            $result = Db::name('activity_join')->where(['active_id' => $id, 'user_id' => $this->member['id']])->delete();
            if ($result) {
                Db::name('activity_offline')->where(['id' => $id])->setDec('joins');
                return $this->pass('已取消报名');
            }
        } else {
            if (sizeof($joinUsers) && $activity['sizes'] == 0) {
                return $this->miss('已有人报名');
            }
            $insert = ['user_id' => $this->member['id'], 'active_id' => $id, 'create_time' => time(), 'paystate' => 1];
            if ($activity['pay'] == 1) {
                $insert['price']     = $activity['price'];
                $insert['paystate']  = 0;
                $insert['paymethod'] = $paymethod;
                $insert['number']    = uniqid('aj');
            }
            $result = Db::name('activity_join')->insert($insert);
            if ($result) {
                if ($activity['pay'] == 1) {
                    switch ((int) $paymethod) {
                    case 2:
                        $action = 'api/Alipay/tradeAppPay';
                        break;
                    case 3:
                        $action = 'api/Wechat/app';
                        if ($w2) {
                            $action = 'api/Wechat2/app';
                        }
                        break;
                    default:
                        return $this->miss('支付方式错误');
                        break;
                    }
                    if ($h5) {
                        return $this->pass(['order_sn' => $insert['number'], 'url' => 'api/wap/pay']);
                    }

                    $insert['url'] = action($action, ['order_sn' => $insert['number'], 'total_amount' => $insert['price'], 'returnJson' => false]);
                } else {
                    Db::name('activity_offline')->where(['id' => $id])->setInc('joins');
                }
                return $this->pass('报名成功' . ($activity['pay'] == 1 ? ',待支付' : ''), 'data', $insert);
            }
        }
        return $this->miss('报名失败,请重试');
    }

    /**
     * @param $id
     * @param $phone
     * @param $code
     * @param $timestamp
     * @return mixed
     */
    public function codejoin($id, $phone, $code = '', $timestamp) {
        $result = _isMobile($phone, '86');
        if ($result['code'] == 400) {
            return $this->miss($result['msg']);
        }
        if (!$code) {
            return $this->miss('请输入验证码');
        }
        $result = Code::verifyCode($phone, $code);
        if ($result['code'] == 400) {
            return $this->miss($result['msg']);
        }

        $user = Db::name('user')->where(['phone' => $phone])->find();
        if (!$user) {
            return $this->miss('请先下载APP进行注册');
        }

        $activity = Db::name('activity_offline')->where(['id' => $id, 'state' => 0, 'delete_time' => 0, 'create_time' => $timestamp])->find();
        if (!$activity || $activity['user_id'] == $user['id']) {
            return $this->miss('操作失败');
        }
        $joinUsers = Db::name('activity_join')->where(['active_id' => $id])->column('user_id');

        if (in_array($user['id'], $joinUsers)) {
            return $this->miss('您已报名');
        } else {
            if (sizeof($joinUsers) && $activity['sizes'] == 0) {
                return $this->miss('报名人数已满');
            }
            $result = Db::name('activity_join')->insert(['user_id' => $user['id'], 'active_id' => $id, 'create_time' => time()]);
            $option = 1;
        }
        if ($result) {
            return $this->pass($option ? '报名成功' : '已取消报名');
        }
        return $this->miss('报名失败,请重试');
    }

    /**
     * @param $page
     * @param $pagesize
     * @return mixed
     */
    public function joined($page = 1, $pagesize = 20) {
        $where      = ['private' => 0, 'delete_time' => 0];
        $active_ids = Db::name('activity_join')->where(['user_id' => $this->member['id']])->order('id desc')->column('active_id');
        if (sizeof($active_ids)) {
            $where['id'] = ['in', $active_ids];
        }
        $data = Db::name('activity_offline')
            ->where($where)
            ->field('id,user_id,content,pay,longitude,latitude,address_name,address_id,activity_time,images,create_time,state')
            ->order('id desc')
            ->limit(($page - 1) * $pagesize, $pagesize)
            ->select();
        $users = Db::name('user')->where(['id' => ['in', array_column($data, 'user_id')]])->cache(60)->column('name,head_portrait,sex,age', 'id');
        foreach ($data as $key => $value) {
            $data[$key]['images']        = explode(',', $value['images'])[0];
            $data[$key]['distance']      = _distance2($this->member['longitude'], $this->member['latitude'], $value['longitude'], $value['latitude']);
            $data[$key]['user']          = $users[$value['user_id']];
            $data[$key]['activity_time'] = date('Y-m-d H:i', strtotime($value['activity_time']));
            $data[$key]['create_time']   = _time($value['create_time']);
            $data[$key]['store']         = Db::name('user_claim_address')->where(['id' => $value['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
            if ($data[$key]['store']) {
                $data[$key]['store']['distance'] = _distance2($this->member['longitude'], $this->member['latitude'], $data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
                unset($data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
            } else {
                $data[$key]['store'] = false;
            }
        }
        return $this->pass($data);
    }

    /**
     * @return mixed
     */
    public function category() {
        $data = Db::name('activity_category')->order('sort desc')->field('id,title')->select();
        return $this->pass($data);
    }

    /**
     * @param $page
     * @param $pagesize
     * @return mixed
     */
    public function joins($page = 1, $pagesize = 20) {
        $data = Db::name('activity_offline')
            ->alias('a')
            ->join('activity_join b', 'b.active_id=a.id')
            ->where(['b.user_id' => $this->member['id']])
            ->field('a.id,a.user_id,a.content,a.pay,a.longitude,a.latitude,a.address_name,a.address_id,a.activity_time,a.images,a.create_time,a.state')
            ->order('b.id desc')
            ->limit(($page - 1) * $pagesize, $pagesize)
            ->select();
        foreach ($data as $key => $value) {
            $data[$key]['images']        = explode(',', $value['images'])[0];
            $data[$key]['distance']      = _distance2($this->member['longitude'], $this->member['latitude'], $value['longitude'], $value['latitude']);
            $data[$key]['activity_time'] = date('Y-m-d H:i', strtotime($value['activity_time']));
            $data[$key]['store']         = Db::name('user_claim_address')->where(['id' => $value['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
            if ($data[$key]['store']) {
                $data[$key]['store']['distance'] = _distance2($this->member['longitude'], $this->member['latitude'], $data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
                unset($data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
            } else {
                $data[$key]['store'] = false;
            }
        }
        return $this->pass($data);
    }

    /**
     * @param $page
     * @param $pagesize
     * @return mixed
     */
    public function myactivity($page = 1, $pagesize = 20) {
        $data = Db::name('activity_offline')
            ->where(['user_id' => $this->member['id'], 'delete_time' => 0])
            ->field('id,user_id,content,pay,longitude,latitude,address_name,address_id,activity_time,images,create_time,state')
            ->order('id desc')
            ->limit(($page - 1) * $pagesize, $pagesize)
            ->select();
        foreach ($data as $key => $value) {
            $data[$key]['images']        = explode(',', $value['images'])[0];
            $data[$key]['distance']      = _distance2($this->member['longitude'], $this->member['latitude'], $value['longitude'], $value['latitude']);
            $data[$key]['join']          = Db::name('activity_join')->where(['active_id' => $value['id'], 'paystate'=>1])->count();
            $data[$key]['activity_time'] = date('Y-m-d H:i', strtotime($value['activity_time']));
            $data[$key]['create_time']   = _time($value['create_time']);
            $data[$key]['store']         = Db::name('user_claim_address')->where(['id' => $value['address_id'], 'user_id' => ['<>', 0], 'state' => 1])->field('id,name,address,longitude,latitude')->find();
            if ($data[$key]['store']) {
                $data[$key]['store']['distance'] = _distance2($this->member['longitude'], $this->member['latitude'], $data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
                unset($data[$key]['store']['longitude'], $data[$key]['store']['latitude']);
            } else {
                $data[$key]['store'] = false;
            }
        }
        return $this->pass($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function confirm($id) {
        $activity = Db::name('activity_offline')->where(['id' => $id, 'state' => 1])->find();
        $data     = Db::name('activity_join')->where(['active_id' => $id, 'user_id' => $this->member['id'], 'paystate' => 1, 'state' => 0])->find();
        if (!$activity || !$data) {
            return $this->miss('操作失败');
        }
        Db::name('activity_join')->where(['id' => $data['id']])->update(['state' => 1, 'update_time' => time()]);
        if ($activity['price'] > 0) {
            YaoqingRecords::index($activity['user_id'], $data['user_id'], $data['price'] * 5, '活动【' . $activity['content'] . '】报名款', 1);
        }
        return $this->pass('操作成功');
    }


    public function lists()
    {
        $page = input('page/d', 1);
        $pagesize = input('pagesize/d', 20);

        $where = ['is_hidden'=>0, 'delete_time'=>0];

        $type = input('type/d', 0);

        switch ((int)$type) {
            case 0:
                $where['start_time'] = ['>', time()];
                break;
            case 1:
                $where['start_time'] = ['<=', time()];
                $where['end_time'] = ['>', time()];
                break;
            case 2:
                $where['end_time'] = ['<', time()];
                break;
            
            default:
                break;
        }

        $data = db('activity_list')
            ->where($where)
            ->order('sort desc, id desc')
            ->page($page)
            ->limit($pagesize)
            ->field('delete_time,sort,update_time,is_hidden', 'filter')
            ->select();

        foreach ($data as &$value) {
            $value['start_time'] = date('Y-m-d H:i', $value['start_time']);
            $value['end_time'] = date('Y-m-d H:i', $value['end_time']);
            $value['create_time'] = date('Y-m-d H:i', $value['create_time']);
        }

        return $this->pass($data);
    }
}